package com.first;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.concurrent.CountDownLatch;

import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = FirstSpringBootApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//在测试类上面指定profiles，可以改变当前spring 的profile，来达到多环境的测试 
//@ActiveProfiles(profiles = "test") 
public class URLHello {
	 @Autowired
     private TestRestTemplate template;
     //注入端口号
     @LocalServerPort
     private int port;
	  
	  @Before
	    public void setUp() throws Exception {  
	        //绑定需要测试的Controller到MockMvcshang  
//	        mockMvc = MockMvcBuilders.standaloneSetup(context).build();  
	    }  
	  
	  
	@Test
	public void contextLoads() {
			int threadCount = 1000;
			int splitPoint = 500;
			CountDownLatch endCount = new CountDownLatch(threadCount);
			CountDownLatch beginCount = new CountDownLatch(1);
			Thread[] threads = new Thread[threadCount];
//	      第一批
			for(int i= 0;i < splitPoint;i++){
				String a = String.valueOf(i);
				threads[i] = new Thread(new  Runnable() {
					public void run() {
						try {
							//等待在一个信号量上，挂起
							beginCount.await();
							 String url = "http://localhost:"+port+"/hello/"+a;
						     MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); 
						     map.add("pageNum", "1");
						     map.add("pageSize", a ); 
						     String result = template.postForObject(url, map, String.class);
						     System.out.println(result);
						     assertNotNull(result);
						     assertThat(result, Matchers.containsString("dhas"));
							endCount.countDown();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				threads[i].start();
			}
			
//			第二批
			for(int i= splitPoint;i < threadCount;i++){
				threads[i] = new Thread(new  Runnable() {
					public void run() {
						try {
							//等待在一个信号量上，挂起
							beginCount.await();
							//用动态代理的方式调用secKill方法
							beginCount.await();
							 String url = "http://localhost:"+port+"/hello";
						     MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); 
						     map.add("pageNum", "1");
						     map.add("pageSize", "3"); 
						     String result = template.postForObject(url, map, String.class);
						     System.out.println(result);
						     assertNotNull(result);
						     assertThat(result, Matchers.containsString("dhas"));
							//testClass.testFunc("test", 10000001L);
							endCount.countDown();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				threads[i].start();

			}
			
			
			long startTime = System.currentTimeMillis();
			//主线程释放开始信号量，并等待结束信号量
			beginCount.countDown();
			
			try {
				//主线程等待结束信号量
				endCount.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
	}

}
